package com.chain.chl.record;

import android.content.Context;

import com.chain.chl.model.card.Card;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by baoyunfeng on 2018/6/9.
 */

public class FavouriteShare {

    public static void addPendingPayRecord(Context context,String unicode,Card card){
        String key = "pending_pay";
        ShareListModel shareListModel = SharePreferenceUtil.getShareListModel(context,key);
        if(shareListModel == null){
            shareListModel = new ShareListModel();
        }
        ShareModel shareModel = new ShareModel(card.getUrl(),unicode);
        shareModel.setExtra(new Gson().toJson(card));
        shareListModel.getShareModelList().add(shareModel);
        SharePreferenceUtil.setShareListModel(context,key,shareListModel);
    }

    public static ShareListModel getPendingPayRecord(Context context){
        String key = "pending_pay";
        ShareListModel shareListModel =  SharePreferenceUtil.getShareListModel(context,key);
        if(shareListModel == null){
            shareListModel = new ShareListModel();
        }
        return shareListModel;
    }

    public static boolean hasPendingPayRecord(Context context,String url){
        for(ShareModel shareModel : getPendingPayRecord(context).getShareModelList()){
            if(shareModel.getUrl().equals(url)){
                return true;
            }
        }
        return false;
    }

    public static String removePendingPayRecord(Context context,String unicode){
        String extra = "";
        String key = "pending_pay";
        ShareListModel shareListModel = getPendingPayRecord(context);
        List<ShareModel> shareModelList = shareListModel.getShareModelList();
        for(int i = 0; i< shareModelList.size(); i++){
            if(shareModelList.get(i).getUnicode().equals(unicode)){
                extra = shareModelList.get(i).getExtra();
                shareModelList.remove(i);
            }
        }
        SharePreferenceUtil.setShareListModel(context,key,shareListModel);
        return extra;
    }


    public static void addAlreadyPayRecord(Context context,String url,String unicode){
        String key = "payed";
        ShareListModel shareListModel = SharePreferenceUtil.getShareListModel(context,key);
        if(shareListModel == null){
            shareListModel = new ShareListModel();
        }
        shareListModel.getShareModelList().add(new ShareModel(url,unicode));
        SharePreferenceUtil.setShareListModel(context,key,shareListModel);
    }

    public static ShareListModel getAlreadyPayRecord(Context context){
        String key = "payed";
        ShareListModel shareListModel =  SharePreferenceUtil.getShareListModel(context,key);
        if(shareListModel == null){
            shareListModel = new ShareListModel();
        }
        return shareListModel;
    }

    public static boolean hasAlreadyPayRecord(Context context,String url){
        for(ShareModel shareModel : getAlreadyPayRecord(context).getShareModelList()){
            if(shareModel.getUrl().equals(url)){
                return true;
            }
        }
        return false;
    }


    public static void removeAlreadyPayRecord(Context context,String unicode){
        String key = "payed";
        ShareListModel shareListModel = getAlreadyPayRecord(context);
        for(int i = 0 ;i<shareListModel.getShareModelList().size();i++){
            if(shareListModel.getShareModelList().get(i).getUnicode().equals(unicode)){
                shareListModel.getShareModelList().remove(i);
            }
        }
        SharePreferenceUtil.setShareListModel(context,key,shareListModel);
    }


}
